trigger CreateCompetitorAuditHistory on CompetitorAuditHistory__c (before insert) {
    
    list<id> auditISVID=new list<id>();
    list<string> brandName=new list<string>();
    
    for(CompetitorAuditHistory__c c:trigger.new){
        auditISVID.add(c.In_store_visit_ID__c);
        brandName.add(c.Brand__c);
    }
    
    list<Competitor__c> CompetitorList=[select id,Brand__c,in_store_visit__c from Competitor__c where Brand__c in:brandName and in_store_visit__c in:auditISVID limit 100];
    
    map<string,id> CompetitorMap=new map<string,id>();
    
    for(Competitor__c c:CompetitorList){
        CompetitorMap.put(c.brand__c+string.valueof(c.in_store_visit__c).substring(0,15),c.id);
    }
    
    
    for(CompetitorAuditHistory__c com:trigger.new){
        if(com.audit__c != null){
            if(CompetitorMap.containskey(com.brand__c+string.valueof(com.In_store_visit_id__c).substring(0,15))){
                com.Competitor_Distribution_Information__c =CompetitorMap.get(com.brand__c+string.valueof(com.In_store_visit_id__c).substring(0,15));
            }
        }
    }
    
}